package com.atguigu.stack;

import java.util.Arrays;

/**
 * @author durant
 * @create 2021-11-12 14:45
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack=new ArrayStack(5);
        System.out.println(arrayStack.isFull());
        System.out.println(arrayStack.isEmpty());
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);
        arrayStack.list();
    }
}
class ArrayStack{
    private int maxSize;
    private int[] stack;
    private int top = -1;//表示栈顶
    public ArrayStack (int Size){
        this.maxSize = Size;
        stack=new int[this.maxSize];
    }
    // 判断栈满
    public boolean isFull(){
        return top == maxSize-1;
    }
    // 判断栈空
    public boolean isEmpty(){
        return top == -1;
    }
    // 入栈
    public void push(int value){
        if (isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top]=value;
    }
    //出栈
    public int pop (){
        if (isEmpty()){

            throw new RuntimeException("栈空");
        }
        int result = stack[top];
        top--;
        return result;
    }
    // 遍历
    public void list(){
        // 判断是否为空
        if (isEmpty()){
            throw new RuntimeException("栈空");
        }
        for (int i = top; i >=0 ; i--) {
            System.out.println(stack[i]);
        }
    }
}
